package com.ruoyi.cmecomon.service;

import com.ruoyi.cmecomon.utils.ColourUtil;
import com.ruoyi.cmecomon.vo.bar.BarVo;
import com.ruoyi.cmecomon.vo.bar.BarValuesVo;
import com.ruoyi.cmecomon.vo.bar.Ordertrendvo;
import com.ruoyi.cmelasercladding.domain.ZsCladdingProcess;
import com.ruoyi.cmelasercladding.domain.ZxLaserCladdingOrderForm;
import com.ruoyi.cmelasercladding.service.IZsCladdingProcessService;
import com.ruoyi.cmelasercladding.service.IZxLaserCladdingOrderFormService;
import com.ruoyi.cmemetal.domain.ZxOrderForm;
import com.ruoyi.cmemetal.domain.ZxPostProcess;
import com.ruoyi.cmemetal.service.IZxOrderFormService;
import com.ruoyi.cmemetal.service.IZxPostProcessService;
import com.ruoyi.cmenonmetal.domain.ZsPostProcess;
import com.ruoyi.cmenonmetal.domain.ZxNonmetalOrderForm;
import com.ruoyi.cmenonmetal.service.IZsPostProcessService;
import com.ruoyi.cmenonmetal.service.IZxNonmetalOrderFormService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Zhang Xu
 * @Date 2021/12/17 21:17
 * @Version 1.0
 */
@Service
public class ChartServiceImpl implements ChartService {


    @Autowired
    private IZxOrderFormService zxOrderFormService;

    @Autowired
    private IZxNonmetalOrderFormService zxNonmetalOrderFormService;

    @Autowired
    private IZxLaserCladdingOrderFormService zxLaserCladdingOrderFormService;


    @Autowired
    private IZxPostProcessService zxPostProcessService;

    @Autowired
    private IZsPostProcessService zsPostProcessService;

    @Autowired
    private IZsCladdingProcessService zsCladdingProcessService;



    @Autowired
    private WorkTimesService workTimesService;

    ColourUtil colourUtil = new ColourUtil();
    @Override
    public BarVo getBarVo(ZxOrderForm zxOrderForm, ZxNonmetalOrderForm zxNonmetalOrderForm, ZxLaserCladdingOrderForm zxLaserCladdingOrderForm) {
        BarVo barVo = new BarVo();
        List<String> names = new ArrayList<>();
        names.add("金属打印");
        names.add("非金属打印");
        names.add("激光熔覆");
        barVo.setNames(names);

        List<BarValuesVo> barValuesVos = new ArrayList<>();

        BarValuesVo barValuesVo = new BarValuesVo();
        List<ZxOrderForm> zxOrderForms = zxOrderFormService.selectZxOrderFormList(zxOrderForm);
        barValuesVo.setValue(zxOrderForms.size());
        barValuesVo.setItemStyle(colourUtil.createItemColour(488));
        barValuesVos.add(barValuesVo);

        BarValuesVo barValuesVo1 = new BarValuesVo();
        List<ZxNonmetalOrderForm> zxNonmetalOrderForms = zxNonmetalOrderFormService.selectZxNonmetalOrderFormList(zxNonmetalOrderForm);
        barValuesVo1.setValue(zxNonmetalOrderForms.size());
        barValuesVo1.setItemStyle(colourUtil.createItemColour(288));
        barValuesVos.add(barValuesVo1);

        BarValuesVo barValuesVo2 = new BarValuesVo();
        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormList(zxLaserCladdingOrderForm);
        barValuesVo2.setValue(zxLaserCladdingOrderForms.size());
        barValuesVo2.setItemStyle(colourUtil.createItemColour(150));
        barValuesVos.add(barValuesVo2);

        barVo.setValues(barValuesVos);

        return barVo;
    }

    @Override
    public Ordertrendvo getOrdertrendvo(ZxOrderForm zxOrderForm, ZxNonmetalOrderForm zxNonmetalOrderForm, ZxLaserCladdingOrderForm zxLaserCladdingOrderForm, String beginTime, String endTime) {
        Ordertrendvo ordertrendvo = new Ordertrendvo();

            Date begindate =null;
            Date enddate = null;
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            try {
                begindate = ft.parse(beginTime);
                enddate = ft.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        List<ZxOrderForm> zxOrderForms = null;
        List<ZxNonmetalOrderForm> zxNonmetalOrderForms = null;
        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = null;

        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df1=new SimpleDateFormat("MM-dd");

        List<String> dates = new ArrayList<>();
        dates.add("Date");
        List<String> metal = new ArrayList<>();
        metal.add("金属打印");
        List<String> nometal = new ArrayList<>();
        nometal.add("非金属打印");
        List<String> lasercladding = new ArrayList<>();
        lasercladding.add("激光熔覆");
        long l = DateUtils.zxgetDate(enddate, begindate);

        for(int i = 0;i <= l;i++){
            zxOrderForms = zxOrderFormService.selectZxOrderBydate(begindate);
            zxNonmetalOrderForms = zxNonmetalOrderFormService.selectNonmetalOrderBydate(begindate);
            zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormBydate(begindate);

            dates.add(df1.format(begindate));
            metal.add(zxOrderForms.size() + "");
            nometal.add(zxNonmetalOrderForms.size() + "");
            lasercladding.add(zxLaserCladdingOrderForms.size() + "");

            try {
                begindate = ft.parse(df.format(begindate.getTime() + 24 * 60 * 60 * 1000));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        ordertrendvo.setDates(dates);
        ordertrendvo.setMetal(metal);
        ordertrendvo.setNometal(nometal);
        ordertrendvo.setLasercladding(lasercladding);


        return ordertrendvo;
    }

    @Override
    public AjaxResult getOrderlist(ZxOrderForm zxOrderForm, ZxNonmetalOrderForm zxNonmetalOrderForm, ZxLaserCladdingOrderForm zxLaserCladdingOrderForm) {
        int orderTotal = 0;

        List<ZxOrderForm> zxOrderForms = zxOrderFormService.selectZxOrderFormList(zxOrderForm);
        if(zxOrderForms.size() != 0){
            orderTotal += zxOrderForms.size();
        }

        List<ZxNonmetalOrderForm> zxNonmetalOrderForms = zxNonmetalOrderFormService.selectZxNonmetalOrderFormList(zxNonmetalOrderForm);
        if(zxNonmetalOrderForms.size() != 0){
            orderTotal += zxNonmetalOrderForms.size();
        }

        List<ZxLaserCladdingOrderForm> zxLaserCladdingOrderForms = zxLaserCladdingOrderFormService.selectZxLaserCladdingOrderFormList(zxLaserCladdingOrderForm);
        if(zxLaserCladdingOrderForms.size() != 0){
            orderTotal += zxLaserCladdingOrderForms.size();
        }

        int completeQuantity =0;


        ZxPostProcess zxPostProcess =new ZxPostProcess();
        for (ZxOrderForm orderForm : zxOrderForms) {
            zxPostProcess.setOrderId(orderForm.getOrderId());
            List<ZxPostProcess> zxPostProcesses = zxPostProcessService.selectZxPostProcessList(zxPostProcess);
            if(zxPostProcesses.size() != 0){
                if(zxPostProcesses.get(0).getIsqualified() ==1){
                    completeQuantity++;
                }
            }
        }

        ZsPostProcess zsPostProcess = new ZsPostProcess();
        for (ZxNonmetalOrderForm nonmetalOrderForm : zxNonmetalOrderForms) {
            zsPostProcess.setOrderId(nonmetalOrderForm.getOrderId());
            List<ZsPostProcess> zsPostProcesses = zsPostProcessService.selectZsPostProcessList(zsPostProcess);
            if(zsPostProcesses.size() != 0){
                if(zsPostProcesses.get(0).getIsqualified() == 1){
                    completeQuantity++;
                }
            }
        }

        ZsCladdingProcess zsCladdingProcess = new ZsCladdingProcess();
        for (ZxLaserCladdingOrderForm laserCladdingOrderForm : zxLaserCladdingOrderForms) {
            zsCladdingProcess.setOrderId(laserCladdingOrderForm.getOrderId());
            List<ZsCladdingProcess> zsCladdingProcesses = zsCladdingProcessService.selectZsCladdingProcessList(zsCladdingProcess);
            if(zsCladdingProcesses.size() != 0){
                if(zsCladdingProcesses.get(0).getIsHandle() == 1){
                    completeQuantity++;
                }
            }
        }



        //构造完成率
        double i = ((double) completeQuantity / (double) orderTotal)*100;
        DecimalFormat decimalFormat= new DecimalFormat( ".00" );
        String completeRate = decimalFormat.format(i);

        AjaxResult success = AjaxResult.success();
        success.put("orderTotal",orderTotal);
        success.put("completeQuantity",completeQuantity);
        success.put("completeRate",completeRate);


        return success;
    }
}
